package com.syh.jn.scm.service.impl.upstream.base;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.upstream.entity.base.UpNewGoodsRequest;
import com.syh.jn.scm.domain.upstream.repository.base.UpNewGoodsRequestRepository;
import com.syh.jn.scm.dto.upstream.base.UpNewGoodsRequestMto;
import com.syh.jn.scm.dto.upstream.base.UpNewGoodsRequestSto;
import com.syh.jn.scm.dto.upstream.base.UpNewGoodsRequestVo;
import com.syh.jn.scm.service.api.upstream.base.UpNewGoodsRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品申请管理
 */
@Service
@Transactional
public class UpNewGoodsRequestServiceImpl extends AbstractServiceImpl<UpNewGoodsRequestSto, UpNewGoodsRequestMto, UpNewGoodsRequestVo, UpNewGoodsRequest, Long> implements UpNewGoodsRequestService {
  @Autowired
  private UpNewGoodsRequestRepository upNewGoodsRequestRepository;

  @Override
  protected AggregateRepository<UpNewGoodsRequest, Long> getAggregateRepository() {
    return upNewGoodsRequestRepository;
  }

  @Override
  public Long save(UpNewGoodsRequestSto sto) {
    ExtAssert.notNull(sto, "商品申请对象为空");
    ValidationUtils.validate(sto);

    //新建商品申请
    UpNewGoodsRequest upNewGoodsRequest = new UpNewGoodsRequest(sto);

    return upNewGoodsRequestRepository.add(upNewGoodsRequest);
  }

  @Override
  public void update(UpNewGoodsRequestMto mto) {
    ExtAssert.notNull(mto, "商品申请对象为空");
    ValidationUtils.validate(mto);

    //根据id查找对应的商品申请信息并判空
    UpNewGoodsRequest upNewGoodsRequest = this.getUpGoodsApplicationById(mto.getId());

    upNewGoodsRequest.modify(mto);
    upNewGoodsRequestRepository.update(upNewGoodsRequest);
//    this.publicEvent(SaleOrderEvent.buildSaleOrderDestory());
  }

  @Override
  public void delete(Long id) {
    //根据id查找对应的商品申请信息并判空
    UpNewGoodsRequest upNewGoodsRequest = this.getUpGoodsApplicationById(id);

    upNewGoodsRequest.destroy();
    upNewGoodsRequestRepository.removeById(id);
  }

  /**
   * 根据上游供应商所有通过审核了的关联商品
   * @param we
   * @return
   */
  @Override
  public List<Long> findGoodsIdByUpStreamSupplier(Wrapper we) {
    List<UpNewGoodsRequest> list = upNewGoodsRequestRepository.getList(we);
    //获取通过审核了的关联商品id
    List<Long> ids = new ArrayList<>();
    list.stream().forEach(upNewGoodsRequest -> {
      ids.add(upNewGoodsRequest.getAssociatedGoodsId());
    });
    return ids;
  }

  /**
   * 根据id查找对应的商品申请信息并判空
   * @param id
   * @return
   */
  private UpNewGoodsRequest getUpGoodsApplicationById(Long id){
    ExtAssert.notNull(id, "id为空");

    UpNewGoodsRequest upNewGoodsRequest = upNewGoodsRequestRepository.getById(id);
    ExtAssert.notNull(upNewGoodsRequest, "对象为空");

    return upNewGoodsRequest;
  }

}
